package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
        // 封装树
        // 1 级分类
        List treeMenu = categoryEntityList.stream()
        .filter(categoryEntity -> {
           return categoryEntity.getParentCid() == 0;   // 找到所有根节点
        })
        .map(categoryEntity -> {
           categoryEntity.setChildren(getChildrens(categoryEntity,categoryEntityList));
           return categoryEntity;
        }) // 设置根节点的子节点  遍历根节点 找到每个根节点的子节点
        .sorted((o1, o2) -> {
           return (o1.getSort()==null?0:o1.getSort())-(o2.getSort()==null?0:o2.getSort());
        })
        .collect(Collectors.toList());
        return treeMenu;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
      //TODO 检查当前删除的菜单是否在其他位置被引用  如果是则不能删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId,paths);

        Collections.reverse(parentPath);

        return (Long[]) parentPath.toArray(new Long[parentPath.size()]);
    }

    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
       this.updateById(category);
       // 更新中间表
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    private List<Long> findParentPath (Long catelogId,List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0) {
           findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    // getChildrens
    private List<CategoryEntity> getChildrens (CategoryEntity root ,List<CategoryEntity> all) {
        List<CategoryEntity> childrens = all.stream()
                .filter(categoryEntity -> {
                    return categoryEntity.getParentCid() == root.getCatId(); // 查找 root的子节点
                })
                .map(categoryEntity -> { // 遍历 root的子节点
                    categoryEntity.setChildren(getChildrens(categoryEntity,all)); //获取 root的子节点的子节点
                    return categoryEntity;
                })
                .sorted((o1, o2) -> {
                    return (o1.getSort()==null?0:o1.getSort()) - (o2.getSort()==null?0:o2.getSort());
                })
                .collect(Collectors.toList());
        return childrens;
    }

}